/**
 * @file 02-intersection-types.ts
 * 
 * 1.3.2 使用较差类型组合类型
 * ConsolidatedGrid 是通过 Grid 和 Margin 组合的一个新的类型
 */

export class Grid {
    Width: number = 0;
    Height: number = 0;
    Padding: number;
    constructor(padding: number) {
        this.Padding = padding;
    }
}

export class Margin {
    Left: number = 0;
    Top: number = 0;
    Width: number = 10;
    Height: number = 20;
    Padding?: number;
}

function ConsolidatedGrid(grid: Grid, margin: Margin): Grid & Margin {
    let consolidatedGrid = <Grid & Margin>{};

    consolidatedGrid.Width = grid.Width + margin.Width;
    consolidatedGrid.Height = grid.Height + margin.Height;
    consolidatedGrid.Left = margin.Left;
    consolidatedGrid.Top = margin.Top;

    consolidatedGrid.Padding = margin.Padding ? margin.Padding : grid.Padding;

    return consolidatedGrid;
}

let grid: Grid = <Grid>{ Height: 20, Width: 10, Padding: 20 };
let margin: Margin = <Margin>{ Left: 5, Top: 5, Height: 20, Width: 10, Padding: 10 };
let x = ConsolidatedGrid(grid, margin);

console.log(`Left : ${x.Left}, Top : ${x.Top}, Width : ${x.Width}, Height : ${x.Height}, Padding : ${x.Padding}`)
